package com.fun.feifun.modules.business.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fun.feifun.base.result.Result;
import com.fun.feifun.base.utils.MD5Util;
import com.fun.feifun.base.utils.format.FormatUtil;
import com.fun.feifun.base.utils.http.HttpUtil;
import com.fun.feifun.modules.business.model.*;
import com.fun.feifun.modules.business.model.param.BusinessOrderOutPutParam;
import com.fun.feifun.modules.business.model.param.BusinessOrderSuccessParam;
import com.fun.feifun.modules.business.model.xml.OrderParam;
import com.fun.feifun.modules.business.model.xml.OrderReturnXML;
import com.fun.feifun.modules.business.service.*;
import com.fun.feifun.modules.sys.parameter.service.ISysParameterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 业务供应商 前端控制器
 * </p>
 *
 * @author wkf
 * @since 2024-06-04
 */
@RestController
@Api(value = "/business/api", tags = "API-供货商")
@Slf4j
@RequestMapping("/business/api")
public class BusinessSupplierAPIController {

    @Autowired
    private IBusinessOrderService service;
    @Autowired
    private IBusinessUserService businessUserService;
    @Autowired
    private IBusinessSupplierService supplierService;
    @Autowired
    private IBusinessGoodsService businessGoodsService;
    @Autowired
    private IBusinessUserGoodsService userGoodsService;
    @Autowired
    private ISysParameterService parameterService;
    @Autowired
    private IBusinessOrderLogService logService;
    /**
     * 通过此接口可以实时查询账户的余额
     * @return
     */
//    @GetMapping(value = "/placeOrder")
    @SaCheckLogin
    @ApiOperation(value = "下单接口")
    public Result placeOrder() {
        String url="http://121.37.177.158:9086/onlinepay.do";
        String returnUrl="";
        String userId="10002504";
        String key="SXpXcscs2HanTWPQcBdF7iJiA2dxme87";
        String productid="101694";
        try {
            OrderParam order=new OrderParam();
            order.setUserid(userId);
            order.setProductid(productid);
            order.setPrice("1");
            order.setNum("1");
            order.setMobile("15696962266");
            order.setSpordertime("20240627133033");
            order.setSporderid("1");
            order.setGascardtel("1");
            order.setGascardname("吴克非");
            order.setSign(order.MD5Sign(key));
            order.setBack_url(returnUrl);
            order.setPaytype("111");
            String responseBody = HttpUtil.odPost(url, order);
            Map<String, String> responsMap = FormatUtil.convertXmlToMap(responseBody);
            return Result.success(responsMap);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("");
        }
    }


    /**
     * 通过此接口可以实时查询账户的余额
     * @return
     */
    @SaCheckLogin
    @GetMapping(value = "/getBalance")
    @ApiOperation(value = "获取商户余额")
    public Result getBalance(@RequestParam(value="userId",defaultValue ="10002504" ) String  userId,
                                    @RequestParam(value="key",defaultValue ="SXpXcscs2HanTWPQcBdF7iJiA2dxme87")  String  key){
        String url="http://121.37.177.158:9086/searchbalance.do";
        Map<String, String> responsMap=new HashMap<>();
        try {
            StringBuilder signStr = new StringBuilder();
            signStr.append("userid="+userId+"&");
            signStr.append("key="+key);
            String sign = MD5Util.encrypt(signStr.toString());
            Map<String,String> map=new HashMap<>();
            map.put("userid",userId);
            map.put("sign",sign);
            String  responseBody= HttpUtil.odPost(url, map);
            responsMap = FormatUtil.convertXmlToMap(responseBody);
            return Result.success(responsMap);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("");
        }
    }

    @SaCheckLogin
    @GetMapping(value = "/getOrederInfo")
    @ApiOperation(value = "获取用户订单接口")
    public Result getOrederInfo(@RequestParam(value="orderId",defaultValue ="10002504" ) String  orderId){
        String url="http://121.37.177.158:9086/searchpay.do";
        try {
            StringBuilder signStr = new StringBuilder();
            signStr.append("userid=10002504&");
            signStr.append("sporderid="+orderId+"&");
            signStr.append("key=SXpXcscs2HanTWPQcBdF7iJiA2dxme87");
            String sign = MD5Util.encrypt(signStr.toString());
            Map<String,String> param=new HashMap<>();
            param.put("userid","10002504");
            param.put("sporderid",orderId);
            param.put("sign",sign);
            String  responseBody= HttpUtil.odPost(url, param);
            Map<String, String> map = FormatUtil.convertXmlToMap(responseBody);
            return Result.success(map);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("");
        }

    }

    @PostMapping(value = "/paymentReturn")
    @ApiOperation(value = "订单-支付成功回调接口")
    public String paymentReturnUrl(HttpServletRequest request) {
        String outsideOrderCode = request.getParameter("orderid");
        System.out.println( request.getParameterNames().toString());
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~回调接口opst("+outsideOrderCode+")~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        try {
            if(StringUtils.isNotEmpty(outsideOrderCode)){
                String resultno = request.getParameter("resultno");
                BusinessOrder order = service.paymentReturn(outsideOrderCode,resultno);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "OK";
    }


    @GetMapping(value = "/paymentReturn")
    @ApiOperation(value = "订单-支付成功回调接口")
    public String paymentReturnUrlGet(HttpServletRequest request) {
        String outsideOrderCode = request.getParameter("orderid");
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~回调接口get("+outsideOrderCode+")~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        try {
            if(StringUtils.isNotEmpty(outsideOrderCode)){
                String resultno = request.getParameter("resultno");
                BusinessOrder order = service.paymentReturn(outsideOrderCode,resultno);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "OK";
    }

    @PostMapping(value = "/testPaymentReturnUrl")
    @ApiOperation(value = "测试-支付成功回调接口")
    public Result testPaymentReturnUrl() {
        String url="http://127.0.0.1:8088/business/api/paymentReturn";
        try {
            OrderReturnXML order=new OrderReturnXML();
            order.setMobile("18487148864");
            order.setNum("1");
            order.setProductid("2");
            order.setSporderid("3");
            order.setOrderid("10");
            order.setFundbalance("5");
            order.setRemark1("6");
            order.setOrdercash("1");
            order.setMerchantsubmittime("merchantsubmittime");
            order.setResultno("1");
            Map<String, String> map = FormatUtil.objToMapStr(order);
            String  responseBody= HttpUtil.odPost(url, map);
            return Result.success(responseBody);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("");
        }
    }

    @PostMapping(value = "/supplierGetOrderApi")
    @ApiOperation(value = "供货商-拉去订单接口")
    public Result< Map<String,Object>> supplierGetOrderApi(@RequestBody BusinessOrderOutPutParam param) {
        return Result.success(service.pullOrder(param));
    }

    @PostMapping(value = "/supplierPaySuccessOrder")
    @ApiOperation(value = "供货商-完成支付订单 ")
    public Result< Map<String,Object>> supplierPaySuccessOrder(@RequestBody BusinessOrderSuccessParam param) {
        service.payPullOrder(param);
        return Result.success();
    }
}

